import requests
import parsel

page_num = 0
for page in range(0, 2020550 + 1, 50):
    page_num += 1
    print('=========正在抓取第{}页的数据=========='.format(page_num))
    base_url = 'https://tieba.baidu.com/f?kw=%E7%BE%8E%E5%A5%B3&ie=utf-8&pn=0'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko'}
    path = r'C:\Users\ASUS\Desktop\图片\ '
    response = requests.get(base_url, headers=headers)
    html_str = response.text
    # print(html_str)

    # 将网页转换为Selector对象
    html = parsel.Selector(html_str)
    title_url = html.xpath(
        '//div[@class="threadlist_lz clearfix"]/div/a/@href').extract()

    second_url = 'https://tieba.baidu.com'
    for url in title_url:
        all_url = second_url + url
        print('当前帖子链接为：', all_url)

        response_2 = requests.get(all_url, headers=headers).text

        response_2_data = parsel.Selector(response_2)
        result_list = response_2_data.xpath(
            '//cc/div/img[@class="BDE_Image"]/@src').extract()

        for li in result_list:
            img_data = requests.get(li, headers=headers).content

            file_name = li.split('/')[-1]
            print('正在下载图片：', file_name)
            with open(path + file_name, 'wb') as f:
                f.write(img_data)
